package com.gobuy.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.gobuy.goods.feign.SkuFeign;
import com.gobuy.goods.pojo.Sku;
import com.gobuy.search.dao.ESManagerMapper;
import com.gobuy.search.pojo.SkuInfo;
import com.gobuy.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Gaozexi on 2020/6/8 22:44
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public void createIndexAndMapping() {
        elasticsearchRestTemplate.createIndex(SkuInfo.class);
    }

    //导入全部的sku信息到es
    @Override
    public void importAll() {
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有Sku数据被查到,无法导入ES索引库");
        }
        //skuList 转换为json
        String jsonString = JSON.toJSONString(skuList);
        //将Json 转换为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonString, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息转换为map
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }


        //导入索引库
        esManagerMapper.saveAll(skuInfoList);


     }

    @Override
    public void importDataToESBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有Sku数据被查到,无法导入ES索引库");
        }
        //skuList 转换为json   58到62行 是为了 利用JSON 将 List<Sku> 转换为List<SkuInfo>
        String jsonString = JSON.toJSONString(skuList);
        //将Json 转换为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonString, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息转换为map
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }
        //TODO 使用lamda表达式改进
//        List<SkuInfo> skuInfoList = skuList.stream().map(sku -> {
//            String skuStr = JSON.toJSONString(sku);
//            SkuInfo skuInfo = JSON.parseObject(skuStr, SkuInfo.class);
//            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
//            skuInfo.setSpecMap(map);
//            return skuInfo;
//        }).collect(Collectors.toList());
//        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void deleteDataFromESBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有Sku数据被查到,无法执行删除索引数据");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }
}
